<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">

<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>在线音乐列表</title>
    <link rel="shortcut icon" href="#" />
    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="js/jquery.min.js"></script>
    <style>
        td, th {
            text-align: center;
        }
    </style>
</head>

<body id="body">
    <div class="container">
        <h3 style="text-align: center">音乐列表</h3>
        <div style="float: left;">
            <form class="form-inline">
                <div class="form-group">
                    <label for="exampleInputName2">歌曲名</label>
                    <input name="name" type="text" class="form-control" id="exampleInputName2">
                </div>
                <button id="submit1" type="button" class="btn btn-primary">查询</button>
            </form>
        </div>
        <div style="float: right;margin-bottom: 15px">
            <a class="btn btn-primary" href="loveMusic.html">喜欢列表</a>
            <a class="btn btn-primary" href="upload.html">添加歌曲</a>
            <a id="delete" class="btn btn-primary">删除选中</a>
        </div>
        <table border="1" class="table table-bordered table-hover">
            <tr class="success">
                <th>选择</th>
                <th>歌名</th>
                <th>歌手</th>
                <th>歌曲</th>
                <th>操作</th>
            </tr>
            <tbody id="info" valign="middle">

            </tbody>
        </table>
    </div>
    <script type="text/javascript">
        <!-- 核心代码实现 -->
        function lode() {
            $.ajax({
                type: 'get',
                url: '/music/findmusic',
                data: {
                    musicName: $("#exampleInputName2").val(),
                },
                success: function (ret) {
                    let data = ret.data;
                    var s = "";
                    for (let i = 0; i < data.length; i++) {
                        var musicUrl = data[i].url + ".mp3";
                        s += "<tr>";
                        s += '<th><input id="' + i + '" rid="'+ data[i].id +'" type="checkbox"></th > ';
                        s += '<td>' + data[i].title + '</td>';
                        s += '<td>' + data[i].singer + '</td>';
                        s += '<td><button class="btn btn-primary" onclick="playerSong(\'' + musicUrl + '\')">播放歌曲</button></td>';
                        s += '<td><button class="btn btn-primary" id="del'+ i +'" onclick="deleteInfo(' + data[i].id + ')">删除</button>' +
                            '<button class="btn btn-primary" id="love'+ i +'" onclick="loveInfo(' + data[i].id + ')">喜欢</button> </td>';
                        s += "</tr>";
                    }
                    // 把拼接好的页面添加到 info 的 id 下
                    $("#info").html(s);
                }
            });
        }
        lode();
        $("#submit1").click(function (){
            lode();
        });
        function deleteInfo(id) {
            $.ajax({
                type: 'post',
                url: '/music/delete',
                data: {
                    id: id
                },
                success: function (data) {
                    console.log(data);
                    if (data.status == -1) {
                        alert(data.message);
                        return;
                    }
                    alert(data.message);
                    location.assign("/list.html");
                },
                error: function () {
                    alert("删除音乐失败!! ");
                }
            });
        }
        function playerSong(musicUrl) {
            var name = musicUrl.substring(musicUrl.lastIndexOf("=") + 1);
            // musicUrl: 播放的音乐地址 name: 播放音乐的名字 0: 播放音乐的开始时间
            SewisePlayer.toPlay(musicUrl, name, 0, true);
        }
        function loveInfo(id) {
            $.ajax({
                type: 'post',
                url: '/lovemusic/likeMusic',
                data: {
                    id: id,
                },
                success: function (data) {
                    console.log(data.message);
                    if (data.status == -1) {
                        alert(data.message);
                        return;
                    }
                    alert(data.message);
                },
                error: function () {
                    alert("添加喜欢音乐失败! ");
                }
            });
        }

        $("#delete").click(function () {
            var ids = new Array();
            var i = 0;
            $("input:checkbox").each(function () {
                if ($(this).is(':checked')) {
                    ids[i] = $(this).attr("rid");
                    i++;
                }
            });
            console.log(ids);
            $.ajax({
                type: "post",
                url: "/music/deleteSel",
                dataType: "json",
                data: {
                    ids: ids,
                },
                success: function (data) {
                    console.log(data);
                    if (data.status == -1) {
                        alert(data.message);
                        return;
                    }
                    alert(data.message);
                    location.assign("/list.html")
                },
                error: function (data) {

                    alert("批量删除音乐失败!! ");
                }
            });
        });
    </script>
</body>
<div style="width: 180px; height: 140px; position:absolute; bottom:10px; right:10px">
    <script type="text/javascript" src="player/sewise.player.min.js"></script>
    <script type="text/javascript">

        SewisePlayer.setup({
            server: "vod",
            type: "mp3",
            //这里是默认的一个网址
            videourl: "http://jackzhang1204.github.io/materials/where_did_time_go.mp3",
            skin: "vodWhite",
            //这里需要设置false
            autostart: "false",
        });
    </script>
</div>

</html>